#!/usr/bin/perl
# Version 0.2.1

$testcase = "rtrlsa_stub";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------

$verdict_1 = 3; # part A
$verdict_2 = 3; # part B
$verdict_3 = 3; # part C
$verdict_4 = 3; # part D

# Open the dumpfile
$success = 1;
open(DUMPFILE, $dumpfile) or $success = 0;

if ($success eq 1) {
	$verdict_1 = 2;
	$verdict_2 = 2;
	$verdict_3 = 2;
	$verdict_4 = 2;
		
	while(<DUMPFILE>) {
		
		# only look at upd packets containing router 0.0.0.1's router LSA, 
		# originated by router 0.0.0.1 (interface 0.0.0.5)
		if(/(.*) 0\.0\.0\.5 > (.*): OSPFv2-ls_upd (.*): rtrid 0\.0\.0\.1 (.*) rtr 0\.0\.0\.1 (.*)/) {
			$lsa_cont = $5;
						
			# now look at the time
			@ti = split(/\./, $1);
			@ts = split(/:/, $ti[0]);
			
			$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60; # secs contains the sim time in seconds
			
			if($secs < 100) {
				# Test A
				
				# look, if there is a type 3 link for network 0.0.0.0
				if($lsa_cont =~ /net 0\.0\.0\.0/) {
					# YES! Test succeeded.
					$verdict_1 = 1;
				}
				
			} elsif($secs < 200) {
				# Test B
				
				# look if there is a type 2 link with 0.0.0.1 as link id and data
				if($lsa_cont =~ /dr 0\.0\.0\.1 if 0\.0\.0\.1/) {
					# YES! Test succeeded.
					$verdict_2 = 1;
				}
				
			} elsif($secs < 300) {
				# Test C
				
				# look, if there is a type 3 link for network 0.0.0.0
				if($lsa_cont =~ /net 0\.0\.0\.0/) {
					# YES! Test succeeded.
					$verdict_3 = 1;
				}
				
			} elsif ($secs > 349) {
				# Test D
				
				# look, if there is a type 3 link for network 0.0.0.0
				if($lsa_cont =~ /net 0\.0\.0\.0/) {
					# YES! Test succeeded.
					$verdict_4 = 1;
				}
			}
		}
		
	}
	close(DUMPFILE);
}



# finally evaluate results and generate report
if ($verdict_1 eq 1) {
	print("Type 3 link for network 1 contained in RUT's router LSA. Test PASSED.\n");
} elsif ($verdict_1 eq 2) {
	print("No type 3 link for network 1 contained in RUT's router LSA. Test FAILED.\n");
} else {
	print("Test results cannot be evaluated. Test INCONCLUSIVE.\n");
}

# not impl. yet in OSPF
$verdict_2 = 3;
if ($verdict_2 eq 1) {
	print("Found type 2 link with RUT as DR. Test PASSED.\n");
} elsif ($verdict_2 eq 2) {
	print("Type 2 link not found in Router LSA. Test FAILED.\n");
} else {
	print("Test results cannot be evaluated. Test INCONCLUSIVE.\n");
}

if ($verdict_3 eq 1) {
	print("Type 3 link for network 1 contained in RUT's router LSA. Test PASSED.\n");
} elsif ($verdict_3 eq 2) {
	print("No type 3 link for network 1 contained in RUT's router LSA. Test FAILED.\n");
} else {
	print("Test results cannot be evaluated. Test INCONCLUSIVE.\n");
}

# not impl. yet in OSPF
$verdict_4 = 3;
if ($verdict_4 eq 1) {
	print("Type 3 link for network 1 contained in RUT's router LSA. Test PASSED.\n");
} elsif ($verdict_4 eq 2) {
	print("No type 3 link for network 1 contained in RUT's router LSA. Test FAILED.\n");
} else {
	print("Test results cannot be evaluated. Test INCONCLUSIVE.\n");
}

$verdict = $verdict_1;
if ($verdict_2 ne $verdict) {
	if($verdict_2 eq 2) {
		$verdict = 2;
	} elsif ($verdict_2 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($verdict_3 ne $verdict) {
	if($verdict_3 eq 2) {
		$verdict = 2;
	} elsif ($verdict_3 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($verdict_4 ne $verdict) {
	if($verdict_4 eq 2) {
		$verdict = 2;
	} elsif ($verdict_4 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");